package com.gooddare.view 
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	import com.gooddare.controller.ApplicationFacade;
	import com.gooddare.locator.ApiLocator;
	import com.gooddare.locator.AssetsLocator;
	import com.gooddare.model.ModelLocator;
	import com.gooddare.model.vo.ApplyVO;
	import com.gooddare.ui.IndexLoading;
	import com.gooddare.ui.LoadingText;
	import com.gooddare.view.component.MainMenuComponent;
	import com.riatimes.mvc.model.vo.DataParamVO;
	import com.riatimes.net.http.HttpService;
	import com.riatimes.util.Reflection;
	import com.riatimes.util.UIUtil;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.utils.Timer;
	import com.greensock.easing.Circ;
	import com.greensock.TweenMax;
	import org.puremvc.as3.interfaces.INotification;
	/**
	 * ...
	 * @author Lyleff
	 */
	public class IndexFrameMediator extends BaseView
	{
		public static var NAME:String = "IndexFrameMediator";
		private var indexFrame:Sprite;
		private var isFirst:Boolean = true;
		private var bulkLoader:BulkLoader;
		private var vo:ApplyVO;
		private var mainMenuArr:Array;
		private var logo:Sprite;
		private var point:Sprite;
		
		private var indexLoading:Sprite;
		private var loadingBack:Sprite;
		private var loadingItem:Sprite;
		private var loadingText:Sprite;
		private var loadingTxt:TextField;
		private var percent:Number;
		public function IndexFrameMediator(name:String=null,viewComponent:Object=null) 
		{
			if (name != null) NAME = name;
			if (viewComponent == null) viewComponent = new Sprite();
			
			super(NAME, viewComponent);
			indexFrame = getView();
			ModelLocator.ROOT.addChild(indexFrame);
			
			var atVO:DataParamVO = new DataParamVO(ApiLocator.API_MAINMENU_JOSN_URL, ApplicationFacade.EVENT_DATA_APPLY_GET_INFO);
			HttpService.getInstance().getData(ApplicationFacade.getInstance(), atVO);
		}
		override public function listNotificationInterests():Array {
			return [
				ApplicationFacade.EVENT_DATA_APPLY_GET_INFO,
				ApplicationFacade.EVENT_APP_MAINMENU_CLICK
			];
		}
		override public function handleNotification(note:INotification):void {
			facade.sendNotification(ApplicationFacade.EVENT_APP_QUITSTAGE_CLICK);
			
			var data:Object = note.getBody();
			switch(note.getName()) {
				case ApplicationFacade.EVENT_DATA_APPLY_GET_INFO:
					var vo:ApplyVO = new ApplyVO(data);
					this.vo = vo;
					initLoading();
				break;
				case ApplicationFacade.EVENT_APP_MAINMENU_CLICK:
					if (isFirst) {
						isFirst = false;
						facade.registerMediator(new HomepageFrameMediator());
					}else {
						TweenMax.killDelayedCallsTo(appHandle);
						var delayNum:Number;
						if (data.toString() == this.vo.list[0].name) {
							moveMainMenu(0);
							delayNum =0.1;
						}else if (data.toString() == this.vo.list[1].name) {
							moveMainMenu(1);
							delayNum = 1.1;
						}else if (data.toString() == this.vo.list[2].name) {
							moveMainMenu(0);
							delayNum = 0.1;
						}else if (data.toString() == this.vo.list[3].name) {
							moveMainMenu(0);
							delayNum = 0.1;
						}
						TweenMax.delayedCall(delayNum,appHandle,[data.toString()]);
					}
				break;
			}
		}
		
		private function moveMainMenu(number:Number):void 
		{
			var i:int = 0;
			switch(number) {
				case 0:
					for (i = 0; i < mainMenuArr.length; i++ ) {
						TweenMax.to(mainMenuArr[mainMenuArr.length-1-i], 0.6, { delay:i*0.1, x:160, y:110 + 70 * (mainMenuArr.length-1-i)+15} );
					}
					break;
					break;
				case 1:
					for (i = 0; i < mainMenuArr.length; i++ ) {
						TweenMax.to(mainMenuArr[i], 0.6, {delay:i*0.1, x:30 + i * (mainMenuArr[i].width + 20), y:0 } );
					}
					break;
			}
		}
		public function appHandle(dataName:String):void {
			if (dataName == this.vo.list[0].name) {
				facade.registerMediator(new HomepageFrameMediator());
			}else if (dataName == this.vo.list[1].name) {
				facade.registerMediator(new PhotopageFrameMediator());
			}else if (dataName == this.vo.list[2].name) {
				trace("2");
			}else if (dataName == this.vo.list[3].name) {
				trace("3");
			}
		}
		private function initLoading():void 
		{
			ModelLocator.ROOT.stage.addEventListener(Event.RESIZE, __resizeHandle);
			createLoadingItem();
			indexLoading.x = 995 / 2;
			__resizeHandle();
			
			bulkLoader = new BulkLoader("main");
			var myLoaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
			
			for (var i:uint = 0; i < vo.list.length; i++ ) {
				bulkLoader.add(vo.list[i].url, { id:vo.list[i].name,"context":myLoaderContext } );
			}

			/*临时快速加载*/
			for (var j:uint = 0; j < 15; j++ ) {
				bulkLoader.add("assets/image/photo/linkimg/"+(j+1)+".jpg", {"context":myLoaderContext } );
			}
			
			///**************************/
			bulkLoader.add(AssetsLocator.ASSETS_INDEX_URL, { "context":myLoaderContext } );
			bulkLoader.add(AssetsLocator.ASSETS_HOMEPAGE_URL, { "context":myLoaderContext } );
			bulkLoader.add(AssetsLocator.ASSETS_PHOTOPAGE_URL, { "context":myLoaderContext } );
			
			bulkLoader.add(ApiLocator.API_HOMEPAGE_JOSN_URL, { "context":myLoaderContext } );
			bulkLoader.addEventListener(BulkProgressEvent.PROGRESS, __loaderProgressHandle);
			bulkLoader.addEventListener(BulkProgressEvent.COMPLETE, __loaderCompleteHandle);
			bulkLoader.start();
		}
		
		private function __resizeHandle(e:Event=null):void 
		{
			var px:int = (995 - ModelLocator.ROOT.stage.stageWidth) / 2;
			TweenMax.to(indexLoading, .5, { alpha:1, x:px } );
			indexLoading.x = px;
			loadingBack.width = ModelLocator.ROOT.stage.stageWidth;
			loadingItem.width=percent* (ModelLocator.ROOT.stage.stageWidth);
		}
		
		private function createLoadingItem():void 
		{
			indexLoading = new IndexLoading();
			loadingText = new LoadingText();
			loadingBack = UIUtil.getElement(indexLoading, "loadingBack") as Sprite;
			loadingItem = UIUtil.getElement(indexLoading, "loadingItem") as Sprite;
			loadingTxt = UIUtil.getElement(loadingText, "loadingTxt") as TextField;
			ModelLocator.ROOT.addChild(indexLoading);
			ModelLocator.ROOT.addChild(loadingText);
			loadingText.y = 320+15;
			loadingText.x = 995 / 2;
			indexLoading.y = 280+15;
		}
		
		private function __loaderProgressHandle(e:BulkProgressEvent):void 
		{
			loadingBack.width = ModelLocator.ROOT.stage.stageWidth;
			
			percent = e._weightPercent;
			if (loadingItem.width < percent * (ModelLocator.ROOT.stage.stageWidth)) {
				TweenMax.to(loadingItem, 0.5, { width:percent * (ModelLocator.ROOT.stage.stageWidth) } );
			}else {
				loadingItem.width = percent * (ModelLocator.ROOT.stage.stageWidth);;
			}
			
			//loadingItem.width=percent* (ModelLocator.ROOT.stage.stageWidth);
			var num:int = int(e.weightPercent * 100);
			loadingTxt.text = num.toString() + "%";
		}
		
		private function __loaderCompleteHandle(e:BulkProgressEvent):void 
		{
			TweenMax.killChildTweensOf(indexFrame, true);
			bulkLoader.removeEventListener(BulkProgressEvent.PROGRESS, __loaderProgressHandle);
			TweenMax.to(indexLoading, .8, { y:320+15, alpha:0,onComplete:indexLoaded } );
			TweenMax.to(loadingText, .8, { y:280+15, alpha:0 } );
			/*createLogo();
			mainMenuAddChild();*/
			
		}
		
		public function indexLoaded():void 
		{
			ModelLocator.ROOT.removeChild(indexLoading);
			ModelLocator.ROOT.removeChild(loadingText);
			createLogo();
			mainMenuAddChild();
		}
		
		private function createLogo():void 
		{
			logo = Reflection.createDisplayObjectInstance(AssetsLocator.ASSETS_INDEX_LOGO_UI) as Sprite;
			indexFrame.addChild(logo);
			logo.x = 995 - logo.width - 20;
			logo.y = 0;
			createDottedLine(25,600,6);
			point.alpha = 0;
			TweenMax.to(point, 5, { alpha:1 } );
		}
		private function createDottedLine(t:uint,len:uint,pad:uint):void 
		{
			point = UIUtil.getElement(logo, "point") as Sprite;
			point.graphics.beginFill(0x000000);
            point.graphics.lineStyle(2, 0x000000);
			
			var timer:Timer = new Timer(t);
			timer.addEventListener(TimerEvent.TIMER, __timerHandle);
			timer.start();
			var i:uint = 0;
			function __timerHandle(e:TimerEvent):void {
				point.graphics.moveTo(0, i);
				point.graphics.lineTo(0, i + 2);
				i += pad;
				if (i >= len) {
					timer.stop();
					timer.removeEventListener(TimerEvent.TIMER, __timerHandle);
				}
			}
            point.graphics.endFill();
			logo.addChild(point);
		}
		private function mainMenuAddChild():void 
		{
			mainMenuArr = new Array();
			
			for (var i:uint = 0; i < vo.list.length; i++ ) {
				var bitmapShoes:Bitmap = bulkLoader.getBitmap(vo.list[i].name);
				var mmc:MainMenuComponent = new MainMenuComponent(bitmapShoes,vo.list[i].name);
				mainMenuArr.push(mmc);
				indexFrame.addChild(mmc);
				mmc.alpha = 0;
				mmc.x = 0;
				mmc.y =80+ 70 * i;
				TweenMax.to(mmc, 0.5, { delay:0.3 * i, alpha:1, x:160, y:110 + 70 * i+15 , ease:Circ.easeOut } );
				if (!i) {
					mmc.mainSelect();
				}
			}
		}
	}

}